package com.csdj.server.controller;

import com.csdj.server.pojo.Admin;
import com.csdj.server.pojo.AdminLoginParam;
import com.csdj.server.pojo.RespBean;
import com.csdj.server.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @Author xyx
 * @Date 2022/12/13 11:26
 * @ClassName: LoginController
 * @Description: 登录的controller类
 * @Version 1.0
 */

//@Controller   跳转页面
//@@RestController  返回json格式数据
@Api(tags = "LoginController登录控制类")//描述这个类是干什么的
@RestController//所有的方法 都返回json格式数据
public class LoginController {

    //注入 IAdminService 登录处理 接口
    @Autowired
    private IAdminService iAdminService;

    @ApiOperation(value = "登录方法返回token")
    @PostMapping("/login")
    public RespBean login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request){
        return iAdminService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword(),adminLoginParam.getCode(),request);
    }

    @ApiOperation(value = "根据用户名获取用户信息")
    @GetMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
        if(principal==null){
            return null;
        }
        String username = principal.getName();
        Admin admin = iAdminService.getAdminInfoByUserName(username);
        admin.setPassword(null);
        //设置 查询用户信息的时候 查询出当前用户的角色列表
        admin.setRoles(iAdminService.getRoles(admin.getId()));
        return admin;
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespBean logout(){
        return RespBean.success("注销成功");
    }
}
